package com.tj.jinzao.widgets;

import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.TextView;

import com.tj.jinzao.utils.ToastUtils;

public class MaxTextLengthFilterIH implements InputFilter {

    private int mMaxLength;
    private String name;
    //构造方法中传入最多能输入的字数
    public MaxTextLengthFilterIH(int max, Context context, String name) {
        mMaxLength = max;
        this.name=name;
        this.context=context;
    }
    private Context context;
    public MaxTextLengthFilterIH(Context context){
        this.context=context;
    }

    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        int keep = mMaxLength - (dest.length() - (dend - dstart));
        if (keep < (end - start)) {
            ToastUtils.showToast(context,name);
        }
        if (keep <= 0) {
            return "";
        } else if (keep >= end - start) {
            return null;
        } else {
            return source.subSequence(start, start + keep);
        }
    }
}
